package net.lab1024.smartadmin.module.business.drivingCycle.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.drivingCycle.domain.dto.DrivingCycleAddDTO;
import net.lab1024.smartadmin.module.business.drivingCycle.domain.dto.DrivingCycleQueryDTO;
import net.lab1024.smartadmin.module.business.drivingCycle.domain.dto.DrivingCycleUpdateDTO;
import net.lab1024.smartadmin.module.business.drivingCycle.domain.vo.DrivingCycleExcelVO;
import net.lab1024.smartadmin.module.business.drivingCycle.domain.vo.DrivingCycleVO;
import net.lab1024.smartadmin.module.business.drivingCycle.service.DrivingCycleService;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ ��ʻ������������ ]
 *
 * @author wuhq
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:22:47
 * @since JDK1.8
 */
@RestController
@Api(tags = {"��ʻ������������"})
public class DrivingCycleController extends BaseController {

    @Autowired
    private DrivingCycleService drivingCycleService;

    @ApiOperation(value = "����id��ȡ����",notes = "@author �Ζ�")
    @GetMapping("/drivingCycle/getById/{id}")
    public ResponseDTO<DrivingCycleVO> getById(@PathVariable Long id) {
        return ResponseDTO.succData(drivingCycleService.getById(id));
    }

    @ApiOperation(value = "����id��ȡ����",notes = "@author 何杺")
    @PostMapping("/drivingCycle/page/queryById/{id}")
    public ResponseDTO<DrivingCycleVO> queryById(@PathVariable Long id) {

        return ResponseDTO.succData(drivingCycleService.getById(id));
    }

    @ApiOperation(value = "��ҳ��ѯ��ʻ������������",notes = "@author wuhq")
    @PostMapping("/drivingCycle/page/query")
    public ResponseDTO<PageResultDTO<DrivingCycleVO>> queryByPage(@RequestBody DrivingCycleQueryDTO queryDTO) {
        if(queryDTO.getCreateUser()!=null){
            queryDTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        }queryDTO.setIsIndexShow(1);
        queryDTO.setMyUserId(SmartRequestTokenUtil.getRequestUserId());
        return drivingCycleService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "�����ʻ������������",notes = "@author wuhq")
    @PostMapping("/drivingCycle/add")
    public ResponseDTO<String> add(@RequestBody @Validated DrivingCycleAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        return drivingCycleService.add(addTO);
    }

    @ApiOperation(value="�޸���ʻ������������",notes = "@author wuhq")
    @PostMapping("/drivingCycle/update")
    public ResponseDTO<String> update(@RequestBody @Validated DrivingCycleUpdateDTO updateDTO){
        return drivingCycleService.update(updateDTO);
    }

    @ApiOperation(value="����ɾ����ʻ������������",notes = "@author wuhq")
    @PostMapping("/drivingCycle/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return drivingCycleService.deleteByIds(idList);
    }

    @ApiOperation(value = "��������", notes = "@author wuhq")
    @PostMapping("/drivingCycle/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //��ѯ����
        List<DrivingCycleExcelVO> drivingCycleList = drivingCycleService.queryBatchExportData(idList);
        //��������
        ExportParams ex = new ExportParams("��ʻ������������", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DrivingCycleExcelVO.class, drivingCycleList);
        downloadExcel("��ʻ������������", workbook, response);
    }

    @ApiOperation(value = "����ȫ��", notes = "@author wuhq")
    @PostMapping("/drivingCycle/export/all")
    public void exportAll(@RequestBody @Validated DrivingCycleQueryDTO queryDTO, HttpServletResponse response) {
        //��ѯ����
        List<DrivingCycleExcelVO> drivingCycleList = drivingCycleService.queryAllExportData(queryDTO);
        //��������
        ExportParams ex = new ExportParams("��ʻ������������", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DrivingCycleExcelVO.class, drivingCycleList);
        downloadExcel("��ʻ������������", workbook, response);
    }

}
